package com.zhang.modules.mianShi.controller;

import com.zhang.common.web.respWrapper.Res;
import com.zhang.modules.api.annotation.ApiPermission;
import com.zhang.modules.mianShi.entity.Region;
import com.zhang.modules.mianShi.entity.cmd.region.RegionAddCmd;
import com.zhang.modules.mianShi.entity.cmd.region.RegionEditCmd;
import com.zhang.modules.mianShi.entity.cmd.region.RegionPageCmd;
import com.zhang.modules.mianShi.entity.dto.region.RegionDto;
import com.zhang.modules.mianShi.service.IRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@RestController
@Api(tags = "区域管理")
@RequestMapping("/api/region")
@ApiPermission("app:region:*")
public class RegionController {

    private IRegionService service;


    public RegionController(IRegionService service) {
        this.service = service;
    }

    @ApiOperation("区域添加")
    @ApiPermission("app:region:add")
    @PostMapping("/add")
    public Res add(@RequestBody @Valid RegionAddCmd addCmd){
        service.save(addCmd, Region::new);
        return Res.ok("添加成功");
    }


    @ApiOperation("区域编辑")
    @ApiPermission("app:region:edit")
    @PostMapping("/edit")
    public Res edit(@RequestBody @Valid RegionEditCmd editCmd){
        service.update(editCmd);
        return Res.ok("更新成功");
    }


    @ApiOperation("区域删除")
    @ApiPermission("app:region:delete")
    @GetMapping("/delete")
    public Res delete(@Valid @NotBlank(message = "id不能为空") String id){
        service.deleteLogic(id);
        return Res.ok("删除成功");
    }

    @ApiOperation("区域列表")
    @ApiPermission("app:region:page")
    @GetMapping("/page")
    public Res page(@Valid RegionPageCmd pageCmd){
        Page<Region> page = service.page(pageCmd);
        return Res.ok(page);
    }

    @ApiOperation("区域列表dto")
    @ApiPermission("app:region:list")
    @GetMapping("/list")
    public Res findAllList(@Valid @NotBlank(message = "id不能为空") String machineId){
        List<RegionDto> page = service.findAllList(machineId);
        return Res.ok(page);
    }


}
